<!DOCTYPE html>
<html lang="en-us">
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
<meta charset="UTF-8">
<title>复杂核心域类型 | Elasticsearch: 权威指南 | Elastic</title>
<link rel="home" href="index.html" title="Elasticsearch: 权威指南">
<link rel="up" href="mapping-analysis.html" title="映射和分析">
<link rel="prev" href="mapping-intro.html" title="映射">
<link rel="next" href="full-body-search.html" title="请求体查询">
<meta name="DC.type" content="Learn/Docs/Elasticsearch/Definitive Guide">
<meta name="DC.subject" content="Elasticsearch">
<meta name="DC.identifier" content="2.x">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.optimizely.com/js/18132920325.js"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="/manifest.json">
    <meta name="apple-mobile-web-app-title" content="Elastic">
    <meta name="application-name" content="Elastic">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
    <meta name="theme-color" content="#ffffff">
    <meta name="naver-site-verification" content="936882c1853b701b3cef3721758d80535413dbfd">
    <meta name="yandex-verification" content="d8a47e95d0972434">
    <meta name="localized" content="true">
    <meta name="st:robots" content="follow,index">
    <meta property="og:image" content="https://www.elastic.co/static/images/elastic-logo-200.png">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" sizes="64x64" href="/favicon_64x64_16bit.png">
    <link rel="apple-touch-icon-precomposed" sizes="32x32" href="/favicon_32x32.png">
    <link rel="apple-touch-icon-precomposed" sizes="16x16" href="/favicon_16x16.png">
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/guide/static/styles.css">
  </head>

  <!--© 2015-2021 Elasticsearch B.V. Copying, publishing and/or distributing without written permission is strictly prohibited.-->

  <body>
    <!-- Google Tag Manager -->
    <script>dataLayer = [];</script><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-58RLH5" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-58RLH5');</script>
    <!-- End Google Tag Manager -->

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12395217-16"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-12395217-16');
    </script>

    <!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->
    <script type="text/javascript">
      (function(){var g=function(e,h,f,g){
      this.get=function(a){for(var a=a+"=",c=document.cookie.split(";"),b=0,e=c.length;b<e;b++){for(var d=c[b];" "==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};
      this.set=function(a,c){var b="",b=new Date;b.setTime(b.getTime()+6048E5);b="; expires="+b.toGMTString();document.cookie=a+"="+c+b+"; path=/; "};
      this.check=function(){var a=this.get(f);if(a)a=a.split(":");else if(100!=e)"v"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(":"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case "v":return!1;case "r":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(":")),!c}return!0};
      this.go=function(){if(this.check()){var a=document.createElement("script");a.type="text/javascript";a.src=g;document.body&&document.body.appendChild(a)}};
      this.start=function(){var a=this;window.addEventListener?window.addEventListener("load",function(){a.go()},!1):window.attachEvent&&window.attachEvent("onload",function(){a.go()})}};
      try{(new g(100,"r","QSI_S_ZN_emkP0oSe9Qrn7kF","https://znemkp0ose9qrn7kf-elastic.siteintercept.qualtrics.com/WRSiteInterceptEngine/?Q_ZID=ZN_emkP0oSe9Qrn7kF")).start()}catch(i){}})();
    </script><div id="ZN_emkP0oSe9Qrn7kF"><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>
    <!--END WEBSITE FEEDBACK SNIPPET-->

    <div id="elastic-nav" style="display:none;"></div>
    <script src="https://www.elastic.co/elastic-nav.js"></script>

    <!-- Subnav -->
    <div>
      <div>
        <div class="tertiary-nav d-none d-md-block">
          <div class="container">
            <div class="p-t-b-15 d-flex justify-content-between nav-container">
              <div class="breadcrum-wrapper"><span><a href="/guide/" style="font-size: 14px; font-weight: 600; color: #000;">Docs</a></span></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="main-container">
      <section id="content">
        <div class="content-wrapper">

          <section id="guide" lang="zh_cn">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                  <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="mapping-analysis.html">映射和分析</a></span>
»
<span class="breadcrumb-node">复杂核心域类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="mapping-intro.html">« 映射</a>
</span>
<span class="next">
<a href="full-body-search.html">请求体查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="complex-core-fields"></a>复杂核心域类型<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h2>
</div></div></div>
<p>除了我们提到的简单标量数据类型， JSON 还有 <code class="literal">null</code> 值，数组，和对象，这些 Elasticsearch 都是支持的。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_多值域"></a>多值域<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>很有可能，我们希望 <code class="literal">tag</code> 域包含多个标签。我们可以以数组的形式索引标签：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{ "tag": [ "search", "nosql" ]}</pre>
</div>
<p>对于数组，没有特殊的映射需求。任何域都可以包含0、1或者多个值，就像全文域分析得到多个词条。</p>
<p>这暗示 <em>数组中所有的值必须是相同数据类型的</em> 。你不能将日期和字符串混在一起。如果你通过索引数组来创建新的域，Elasticsearch 会用数组中第一个值的数据类型作为这个域的 <code class="literal">类型</code> 。</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>当你从 Elasticsearch 得到一个文档，每个数组的顺序和你当初索引文档时一样。你得到的 <code class="literal">_source</code> 域，包含与你索引的一模一样的 JSON 文档。</p>
<p>但是，数组是以多值域 <em>索引的</em>—可以搜索，但是无序的。  在搜索的时候，你不能指定 “第一个” 或者 “最后一个”。 更确切的说，把数组想象成 <em>装在袋子里的值</em> 。
</p>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_空域"></a>空域<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>当然，数组可以为空。这相当于存在零值。
事实上，在 Lucene 中是不能存储 <code class="literal">null</code> 值的，所以我们认为存在 <code class="literal">null</code> 值的域为空域。</p>
<p>下面三种域被认为是空的，它们将不会被索引：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"null_value":               null,
"empty_array":              [],
"array_with_null_value":    [ null ]</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="inner-objects"></a>多层级对象<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>我们讨论的最后一个 JSON 原生数据类是 <em>对象</em> -- 在其他语言中称为哈希，哈希 map，字典或者关联数组。</p>
<p><em>内部对象</em> 经常用于嵌入一个实体或对象到其它对象中。例如，与其在 <code class="literal">tweet</code> 文档中包含 <code class="literal">user_name</code> 和 <code class="literal">user_id</code> 域，我们也可以这样写：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "tweet":            "Elasticsearch is very flexible",
    "user": {
        "id":           "@johnsmith",
        "gender":       "male",
        "age":          26,
        "name": {
            "full":     "John Smith",
            "first":    "John",
            "last":     "Smith"
        }
    }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_内部对象的映射"></a>内部对象的映射<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Elasticsearch 会动态监测新的对象域并映射它们为 <code class="literal">对象</code> ，在  <code class="literal">properties</code> 属性下列出内部域：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "gb": {
    "tweet": { <a id="CO21-1"></a><i class="conum" data-value="1"></i>
      "properties": {
        "tweet":            { "type": "string" },
        "user": { <a id="CO21-2"></a><i class="conum" data-value="2"></i>
          "type":             "object",
          "properties": {
            "id":           { "type": "string" },
            "gender":       { "type": "string" },
            "age":          { "type": "long"   },
            "name":   { <a id="CO21-3"></a><i class="conum" data-value="2"></i>
              "type":         "object",
              "properties": {
                "full":     { "type": "string" },
                "first":    { "type": "string" },
                "last":     { "type": "string" }
              }
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO21-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>根对象</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO21-2"><i class="conum" data-value="2"></i></a><a href="#CO21-3"></a></p>
</td>
<td align="left" valign="top">
<p>内部对象</p>
</td>
</tr>
</table>
</div>
<p><code class="literal">user</code> 和 <code class="literal">name</code> 域的映射结构与 <code class="literal">tweet</code> 类型的相同。事实上， <code class="literal">type</code> 映射只是一种特殊的 <code class="literal">对象</code> 映射，我们称之为 <em>根对象</em> 。除了它有一些文档元数据的特殊顶级域，例如 <code class="literal">_source</code> 和 <code class="literal">_all</code> 域，它和其他对象一样。</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_内部对象是如何索引的"></a>内部对象是如何索引的<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Lucene 不理解内部对象。 Lucene 文档是由一组键值对列表组成的。为了能让 Elasticsearch 有效地索引内部类，它把我们的文档转化成这样：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "tweet":            [elasticsearch, flexible, very],
    "user.id":          [@johnsmith],
    "user.gender":      [male],
    "user.age":         [26],
    "user.name.full":   [john, smith],
    "user.name.first":  [john],
    "user.name.last":   [smith]
}</pre>
</div>
<p><em>内部域</em> 可以通过名称引用（例如， <code class="literal">first</code> ）。为了区分同名的两个域，我们可以使用全 <em>路径</em> （例如， <code class="literal">user.name.first</code> ） 或 <code class="literal">type</code> 名加路径（ <code class="literal">tweet.user.name.first</code> ）。</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>在前面简单扁平的文档中，没有 <code class="literal">user</code>
和 <code class="literal">user.name</code> 域。Lucene 索引只有标量和简单值，没有复杂数据结构。</p>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="object-arrays"></a>内部对象数组<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/052_Mapping_Analysis/50_Complex_datatypes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>最后，考虑包含内部对象的数组是如何被索引的。
假设我们有个 <code class="literal">followers</code> 数组：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "followers": [
        { "age": 35, "name": "Mary White"},
        { "age": 26, "name": "Alex Jones"},
        { "age": 19, "name": "Lisa Smith"}
    ]
}</pre>
</div>
<p>这个文档会像我们之前描述的那样被扁平化处理，结果如下所示：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "followers.age":    [19, 26, 35],
    "followers.name":   [alex, jones, lisa, smith, mary, white]
}</pre>
</div>
<p><code class="literal">{age: 35}</code> 和 <code class="literal">{name: Mary White}</code> 之间的相关性已经丢失了，因为每个多值域只是一包无序的值，而不是有序数组。这足以让我们问，“有一个26岁的追随者？”</p>
<p>但是我们不能得到一个准确的答案：“是否有一个26岁 <em>名字叫 Alex Jones</em> 的追随者？”</p>
<p>相关内部对象被称为 <em>nested</em> 对象，可以回答上面的查询，我们稍后会在<a class="xref" href="nested-objects.html" title="嵌套对象"><em>嵌套对象</em></a>中介绍它。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="mapping-intro.html">« 映射</a>
</span>
<span class="next">
<a href="full-body-search.html">请求体查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                  <div id="rtpcontainer" style="display: block;">
                    <div class="mktg-promo">
                      <h3>Most Popular</h3>
                      <ul class="icons">
                        <li class="icon-elasticsearch-white"><a href="https://www.elastic.co/webinars/getting-started-elasticsearch?baymax=default&amp;elektra=docs&amp;storm=top-video">Get Started with Elasticsearch: Video</a></li>
                        <li class="icon-kibana-white"><a href="https://www.elastic.co/webinars/getting-started-kibana?baymax=default&amp;elektra=docs&amp;storm=top-video">Intro to Kibana: Video</a></li>
                        <li class="icon-logstash-white"><a href="https://www.elastic.co/webinars/introduction-elk-stack?baymax=default&amp;elektra=docs&amp;storm=top-video">ELK for Logs &amp; Metrics: Video</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>

        </div>


<div id="elastic-footer"></div>
<script src="https://www.elastic.co/elastic-footer.js"></script>
<!-- Footer Section end-->

      </section>
    </div>

<script src="/guide/static/jquery.js"></script>
<script type="text/javascript" src="/guide/static/docs.js"></script>
<script type="text/javascript">
  window.initial_state = {}</script>
  </body>
</html>
